#include "hplctimeprogramtabledelegate.h"
#include <QStandardItemModel>
#include <QLineEdit>
#include <QComboBox>
#include <QDebug>
#include <QtCore/qmetaobject.h>

HplcTimeProgramTableDelegate::HplcTimeProgramTableDelegate(QObject *parent)
    : QItemDelegate(parent)
{

}

HplcTimeProgramTableDelegate::~HplcTimeProgramTableDelegate()
{

}

QWidget *HplcTimeProgramTableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    if (index.column() == 8) // isZero
    {
        QComboBox *cb = new QComboBox(parent);
        cb->addItem(tr(""), -1);
        cb->addItem(tr("Yes"), 1);
        return cb;
    }
    else if (index.column() == 9) // range
    {
        QComboBox *cb = new QComboBox(parent);
        cb->addItem(tr(""), -1);
        cb->addItem(tr("0.0001"), 0);
        cb->addItem(tr("0.0002"), 1);
        cb->addItem(tr("0.0005"), 2);
        cb->addItem(tr("0.001"), 3);
        cb->addItem(tr("0.002"), 4);
        cb->addItem(tr("0.005"), 5);
        cb->addItem(tr("0.01"), 6);
        cb->addItem(tr("0.02"), 7);
        cb->addItem(tr("0.05"), 8);
        cb->addItem(tr("0.1"), 9);
        cb->addItem(tr("0.2"), 10);
        cb->addItem(tr("0.5"), 11);
        cb->addItem(tr("1.0"), 12);
        cb->addItem(tr("2.0"), 13);
        cb->addItem(tr("5.0"), 14);
        cb->addItem(tr("10.0"), 15);
        return cb;
    }
    else
        return new QLineEdit(parent);
}

void HplcTimeProgramTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (!index.isValid())
        return;

    if (index.column() == 8) // isZero
    {
        QComboBox *cb = static_cast<QComboBox*>(editor);
        QVariant v = index.data(Qt::DisplayRole);
        QString text = v.toString();
        cb->setCurrentText(text);
    }
    else if (index.column() == 9) // range
    {
        QComboBox *cb = qobject_cast<QComboBox*>(editor);
        if (cb)
        {
            QVariant v = index.data(Qt::DisplayRole);
            QString text = v.toString();
            cb->setCurrentText(text);
        }
        else
        {
            QItemDelegate::setEditorData(editor, index);
        }
    }
    else
    {
        QItemDelegate::setEditorData(editor, index);
    }
}

void HplcTimeProgramTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (!index.isValid())
        return;

    if (index.column() == 8) // isZero
    {
        QComboBox *cb = static_cast<QComboBox*>(editor);
        QString text = cb->currentText();
        model->setData(index, text, Qt::DisplayRole);
    }
    else if (index.column() == 9) // range
    {
        QComboBox *cb = static_cast<QComboBox*>(editor);
        QString text = cb->currentText();
        model->setData(index, text, Qt::DisplayRole);
    }
    else
    {
        QItemDelegate::setModelData(editor, model, index);
    }

    if (index.column() == 0)//时间
    {
        if (index.row() == 0)//第一行必须是0
        {
            model->setData(model->index(index.row(), index.column()), 0);
        }
        else
        {
            float timePrev = model->data(model->index(index.row() - 1, index.column())).toFloat();
            float time = model->data(model->index(index.row(), index.column())).toFloat();
            if(time < timePrev)
                model->setData(model->index(index.row(), index.column()), timePrev);
        }

        //float time = model->data(model->index(index.row(), index.column())).toFloat();
        //model->setData(model->index(index.row(), index.column()), QString::number(time, 'f', 2));
    }
    else if (index.column() == 2)//percentA
    {
        quint8 percentA = model->data(model->index(index.row(), index.column())).toUInt();
        model->setData(model->index(index.row(), index.column() + 1), QVariant(100 - percentA));
        model->setData(model->index(index.row(), index.column() + 2), 0);
        model->setData(model->index(index.row(), index.column() + 3), 0);
    }
    else if (index.column() == 3)//percentB
    {
        quint8 percentA = model->data(model->index(index.row(), index.column() - 1)).toUInt();
        quint8 percentB = model->data(model->index(index.row(), index.column())).toUInt();
        if (percentA + percentB > 100)
            percentB = 100 - percentA;
        model->setData(model->index(index.row(), index.column()), percentB);
        model->setData(model->index(index.row(), index.column() + 1), 100 - percentA - percentB);
        model->setData(model->index(index.row(), index.column() + 2), 0);
    }
    else if (index.column() == 4)//percentC
    {
        quint8 percentA = model->data(model->index(index.row(), index.column() - 2)).toUInt();
        quint8 percentB = model->data(model->index(index.row(), index.column() - 1)).toUInt();
        quint8 percentC = model->data(model->index(index.row(), index.column())).toUInt();
        if (percentA + percentB + percentC > 100)
            percentC = 100 - percentA - percentB;
        model->setData(model->index(index.row(), index.column()), percentC);
        model->setData(model->index(index.row(), index.column() + 1), 100 - percentA - percentB - percentC);
    }
    else if (index.column() == 5)//percentD
    {
        quint8 percentA = model->data(model->index(index.row(), index.column() - 3)).toUInt();
        quint8 percentB = model->data(model->index(index.row(), index.column() - 2)).toUInt();
        quint8 percentC = model->data(model->index(index.row(), index.column() - 1)).toUInt();
        quint8 percentD = model->data(model->index(index.row(), index.column())).toUInt();
        if (percentA + percentB + percentC + percentD != 100)
            percentD = 100 - percentA - percentB - percentC;
        model->setData(model->index(index.row(), index.column()), percentD);
    }

    model->submit();
}
